package com.suzj.extraenchantments.enchant.Ench_02;

import com.suzj.extraenchantments.Config;

import com.suzj.extraenchantments.util.EffectUtil;
import java.util.List;

import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentCategory;



public class EnchantmentVoidBlessing extends Enchantment {
    public EnchantmentVoidBlessing() {
        super(Enchantment.Rarity.VERY_RARE, EnchantmentCategory.WEAPON,  new EquipmentSlot[]{EquipmentSlot.MAINHAND});
    }

    public int getMaxLevel() {
        if (((Boolean) Config.SERVER.isVoidBlessing.get()).booleanValue()) {
            return 2;
        }
        return 0;
    }

    public int getMinCost(int enchantmentLevel) {
        return 30;
    }

    public int getMaxCost(int enchantmentLevel) {
        return 50;
    }

    public void doPostAttack(LivingEntity attacker, Entity victimq, int level) {
        if (level <= 0 || attacker == null || victimq == null || !(victimq instanceof LivingEntity)) {
            return;
        }
        LivingEntity victim = (LivingEntity) victimq;
        List<MobEffectInstance> negativeEffects =  victim.getActiveEffects().stream().filter(effectInstance -> {
            return effectInstance.getEffect().getCategory() == MobEffectCategory.HARMFUL;
        }).filter(effectInstance2 -> {
            return effectInstance2.isCurativeItem(new ItemStack(Items.MILK_BUCKET));
        }).filter(EffectUtil::isEffectShown).toList();
        for (var effect : negativeEffects) {
            int effectlevel = effect.getAmplifier();
            int duration = effect.getDuration();
           var effect1 = effect.getEffect();
            victim.removeEffect(effect1);
            int i = (effectlevel + level) - 1;
            if (i > level) {
                i = effectlevel;
            }
            var copyeffect = new MobEffectInstance(effect1, duration + (level * 60), i);
            victim.addEffect(copyeffect);
            if (victim.isOnFire()) {
                int time = victim.getRemainingFireTicks();
                victim.setSecondsOnFire(time + (level * 60));
            }
        }
    }
}